Grokking Amazon Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Amazon Easy Problems
Redistribute Characters to Make All Strings Equal (easy)
Solution: Redistribute Characters to Make All Strings Equal
Longest Common Prefix (easy)
Solution: Longest Common Prefix
Make Array Zero by Subtracting Equal Amounts (easy)
Solution: Make Array Zero by Subtracting Equal Amounts
Leaf-Similar Trees (easy)
Solution: Leaf-Similar Trees
Path Crossing (easy)
Solution: Path Crossing
Special Array With X Elements Greater Than or Equal X (easy)
Solution: Special Array With X Elements Greater Than or Equal X
Relative Sort Array (easy)
Solution: Relative Sort Array
Transpose Matrix (easy)
Solution: Transpose Matrix
Find Common Characters (easy)
Solution: Find Common Characters
Fizz Buzz (easy)
Solution: Fizz Buzz
Increasing Order Search Tree (easy)
Solution: Increasing Order Search Tree
Largest Odd Number in String (easy)
Solution: Largest Odd Number in String
Best Time to Buy and Sell (easy)
Solution: Best Time to Buy and Sell Stock
Take Gifts From the Richest Pile(easy)
Solution: Take Gifts From the Richest Pile
Remove Duplicates from Sorted List (easy)
Solution: Remove Duplicates from Sorted List
Amazon Medium Problems
Maximum Length of Subarray With Positive Product (medium)
Solution: Maximum Length of Subarray With Positive Product
Largest Submatrix With Rearrangements (medium)
Solution: Largest Submatrix With Rearrangements
Count the Number of Good Subarrays (medium)
Solution: Count the Number of Good Subarrays
Minimum Suffix Flips (medium)
Solution: Minimum Suffix Flips
Minimum Number of Operations to Make Array Empty (medium)
Solution: Minimum Number of Operations to Make Array Empty
Next Greater Element II (medium)
Solution: Next Greater Element II
Make Sum Divisible by P (medium)
Solution: Make Sum Divisible by P
Rotting Oranges (medium)
Solution: Rotting Oranges
Minimum Deletions to Make Character Frequencies Unique (medium)
Solution: Minimum Deletions to Make Character Frequencies Unique
Minimum Adjacent Swaps to Make a Valid Array (medium)
Solution: Minimum Adjacent Swaps to Make a Valid Array
Reverse Integer (medium)
Solution: Reverse Integer
Maximize Distance to Closest Person (medium)
Solution: Maximize Distance to Closest Person
Split Linked List in Parts (medium)
Solution: Split Linked List in Parts
Maximum Difference Between Node and Ancestor (medium)
Solution: Maximum Difference Between Node and Ancestor
Deepest Leaves Sum (medium)
Solution: Deepest Leaves Sum
The kth Factor of n (medium)
Solution: The kth Factor of n
Coin Change II (medium)
Solution: Coin Change II
Sum of Subarray Minimums (medium)
Solution: Sum of Subarray Minimums
Valid Sudoku (medium)
Solution: Valid Sudoku
Longest Increasing Subsequence (medium)
Solution: Longest Increasing Subsequence
Next Permutation (medium)
Solution: Next Permutation
Subsets (medium)
Solution: Subsets
Combination Sum (medium)
Solution: Combination Sum
Number of Provinces (medium)
Solution: Number of Provinces
Number of Islands (medium)
Solution: Number of Islands
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Number of Valid Subarrays (hard)
Solution: Number of Valid Subarrays
Count Vowels Permutation (hard)
Solution: Count Vowels Permutation
Concatenated Words (hard)
Solution: Concatenated Words
Minimum Replacements to Sort the Array (hard)
Solution: Minimum Replacements to Sort the Array
Count All Valid Pickup and Delivery Options (hard)
Solution: Count All Valid Pickup and Delivery Options
Length of the Longest Valid Substring (hard)
Solution: Length of the Longest Valid Substring
Count Array Pairs Divisible by K (hard)
Solution: Count Array Pairs Divisible by K
Word Ladder (hard)
Solution: Word Ladder
Path with Maximum Sum (hard)
Solution: Path with Maximum Sum
Solution: Reverse Integer

Problem Statement

Given a signed 32-bit integer x, return the reverse of x. If the reversed value of x goes beyond the signed 32-bit integer range [-231, 231 - 1], then return 0.

A reverse of x should not contain leading zeros.

Examples

  • Example 1:

    • Input: x = 123
    • Expected Output: 321
    • Justification: Reversing the digits of 123 results in 321, which is within the 32-bit signed integer range.
  • Example 2:

    • Input: x = -456432
    • Expected Output: -234654
    • Justification: Reversing the digits of -456432 results in -234654, maintaining the negative sign and staying within the 32-bit signed integer range.
  • Example 3:

    • Input: x = 1200
    • Expected Output: 21
    • Justification: Reversing the digits of 1200 results in 0021, but leading zeros are dropped, resulting in 21.

Solution

To solve this problem, we'll take a mathematical approach to reverse the digits of the given integer. This approach is effective because it directly manipulates the numerical value, allowing us to easily handle both positive and negative numbers and check for overflow conditions. The essence of the solution is to iteratively extract the last digit of the current number, append it to the reversed number, and then remove that digit from the original number by division. This process repeats until all digits have been processed.

The key to handling overflow is to check if the new reversed number, when multiplied back by 10 (to add a new digit), would exceed the 32-bit integer limit. By performing this check before actually appending the next digit, we can ensure that our function never produces an overflowed result. This method is not only straightforward but also efficient, as it requires no additional data structures and operates in linear time complexity relative to the number of digits in the input.

Step-by-step Algorithm

  • Initialize a variable reversed to store the reversed number, starting at 0.
  • While the input number x is not 0:
    • Extract the last digit of x by taking x % 10.
    • Check for overflow/underflow before appending the digit to reversed. If appending would cause overflow or underflow, return 0.
    • Append the digit to reversed by multiplying reversed by 10 and adding the extracted digit.
    • Remove the last digit from x by dividing it by 10.
  • After the loop, if reversed is within the 32-bit signed integer range, return reversed. Otherwise, return 0.

Algorithm Walkthrough

Let's consider the Input: -456432

  1. Initialize Variables:

    • Set reversed to 0. This variable will hold our reversed number as we build it.
    • The input x is (-456432).
  2. Iteration 1:

    • Extract the last digit: (x % 10 = -2).
    • Append -2 to reversed: (reversed = reversed * 10 - 2 = -2).
    • Update x by removing the last digit: (x = x / 10 = -45643).
  3. Iteration 2:

    • Extract the last digit: (x % 10 = -3).
    • Append -3 to reversed: (reversed = -2 * 10 - 3 = -23).
    • Update x: (x = x / 10 = -4564).
  4. Iteration 3:

    • Extract the last digit: (x % 10 = -4).
    • Append -4 to reversed: (reversed = -23 * 10 - 4 = -234).
    • Update x: (x = x / 10 = -456).
  5. Iteration 4:

    • Extract the last digit: (x % 10 = -6).
    • Append -6 to reversed: (reversed = -234 * 10 - 6 = -2346).
    • Update x: (x = x / 10 = -45).
  6. Iteration 5:

    • Extract the last digit: (x % 10 = -5).
    • Append -5 to reversed: (reversed = -2346 * 10 - 5 = -23465).
    • Update x: (x = x / 10 = -4).
  7. Iteration 6:

    • Extract the last digit: (x % 10 = -4).
    • Append -4 to reversed: (reversed = -23465 * 10 - 4 = -234654).
    • Update x: (x = x / 10 = 0).
  8. Final Check and Return:

    • At this point, x is 0, so we exit the loop.
    • Return reversed, which is (-234654).

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

The time complexity of the reverse integer algorithm is , where (n) is the value of the input integer. This is because the algorithm processes each digit of the input number exactly once, and the number of digits in a number (n) is proportional to .

Space Complexity

The space complexity of the algorithm is , indicating constant space usage. This is because the algorithm uses a fixed amount of space (a few variables) regardless of the input size.

Reverse Integer (medium)
Maximize Distance to Closest Person (medium)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-step Algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity